<?php
namespace app\controller;

use app\BaseController;
use app\controller\model\Tables;
use liliuwei\think\Auth;
use think\facade\Session;
use think\facade\Db;
class Article extends BaseController
{
    protected $getpath="";
    protected $gobaluser="";
    protected $tables;
    public function __construct(){
        checkuser();
        $this->gobaluser=Session::get("uid");
        $this->tables=new Tables();
    }
    public function groups(){
        return (new Classes())->classes(1);
    }
    public function authruleedit(){
        return (new Classes())->authruleedit();
    }
    public function add(){
        $id = request()->param("id");
        $id<1?$id=0:'';
        return view("add")->assign(['data'=>r_one($this->tables->article,"id=$id"),'cids'=>r_classes(222)]);
    }
    public function index(){
        $id = request()->param("id");
        $id<1?$id=0:'';
        return view("index")->assign(['data'=>r_one($this->tables->article,"id=$id"),'cids'=>r_classes(222)]);
    }
    public function getList(){
        $cid = request()->param("cid");
        $cid>0?$where="cid=$cid":$where='1=1';
        $status = request()->param("status");
        $status==-1?$where.=" and status=-1":$where.=' and status=1';
        $cid = request()->param("cid");
        $cid>1?$where.=" and cid=$cid":'';
        $title = request()->param("title");
        $title!=''?$where.=" and title like '%$title%'":'';
        $res = getpagedata($this->tables->article,"",true,$where);
        $count = $res[1];
        $res = $res[0];
        $cids = r_classes(222);
        $res = $res->all();
        foreach ($res as $k=>$v){
            $res[$k]['cid']=rclasstitle($cids,$v['cid']);
        }
        pagejson($res,0,'',$count);
    }
    public function save(){
        $data = request()->param();
        unset($data['id']);
        $id = request()->param("id");
        !isset($data['title']) || $data['title']==''?echojson([],1,'标题必填'):'';
        !isset($data['content']) || $data['content']==''?echojson([],1,'内容不能为空'):'';

        $data['content'] = str_replace('"/uploads/','"'.apiURLHTTP().apiURL().'/uploads/',$data['content']);
        $data['content'] = str_replace('<img src=','<img width=100%  src=',$data['content']);
        $data['content'] = str_replace('<img alt=','<img width=100%  alt=',$data['content']);


        $data['addtime']=date("Y-m-d H:i:s");
        if($id>0){
            $re = r_one($this->tables->article,"id=$id");
            if($re){
                DB::name($this->tables->article)->where("id",$id)->update($data);
                echojson([],0,'更新成功');
            }else{
                echojson([],1,'ID错误');
            }
        }else{
            $re = DB::name($this->tables->article)->insert($data);
            if($re){
                echojson([],0,'添加成功');
            }else{
                echojson([],1,'ID错误');
            }
        }
    }


}
